﻿Imports System.IO
Public Class libaryview
    Shared listbox1 As ListBox
    ''' <summary>
    ''' The path of the root directory you want to be "the image libary". You can go deeper to its child directories.
    ''' </summary>
    ''' <param name="LibaryRoot"></param>
    ''' The listview you want to be filled by the image list.
    ''' <param name="TargetListView"></param>
    Public Shared Sub refresh(LibaryRoot As String, TargetListView As ListView)
        Dim FileimgList As ImageList

        Try

            TargetListView.Items.Clear()
            listbox1.Items.Clear()
            FileimgList.Images.Clear()
            FileimgList.Images.Add(My.Resources.opensmall)
            Dim imgindex As Integer = 1
            Dim dirlist As New List(Of Directory)

            Dim slash As Char = "\"
            Dim Dirs() As String = Directory.GetDirectories(LibaryRoot)
            For Each Dir As String In Dirs
                TargetListView.Items.Insert(TargetListView.Items.Count, Dir.Split("\").Last(), 0).Group = TargetListView.Groups.Item(0)
                Dim di As New DirectoryInfo(Dir)
                listbox1.Items.Add(Dir)
                TargetListView.Items.Item(TargetListView.Items.Count - 1).SubItems.Add(di.CreationTime, Color.Gray, Color.White, TargetListView.Font)
                TargetListView.Items.Item(TargetListView.Items.Count - 1).SubItems.Add(di.LastWriteTime, Color.Gray, Color.White, TargetListView.Font)
            Next
            Dim Files() As String = Directory.GetFiles(LibaryRoot, "*.jpg")
            For Each img As String In Files
                Dim ii As New IO.FileInfo(img)
                listbox1.Items.Add(img)
                FileimgList.Images.Add(Image.FromFile(img))
                TargetListView.Items.Insert(TargetListView.Items.Count, ii.Name, imgindex).Group = TargetListView.Groups.Item(1)
                TargetListView.Items.Item(TargetListView.Items.Count - 1).SubItems.Add(ii.CreationTime, Color.Gray, Color.White, TargetListView.Font)
                TargetListView.Items.Item(TargetListView.Items.Count - 1).SubItems.Add(ii.LastWriteTime, Color.Gray, Color.White, TargetListView.Font)
                imgindex = imgindex + 1
            Next
            For Each item As ListViewItem In TargetListView.Items
                item.Font = New Font("Segoe UI Light", 15, FontStyle.Regular)
                item.ForeColor = System.Drawing.SystemColors.HotTrack


            Next
        Catch ex As OutOfMemoryException
            Throw New Exception("There is not enough memory for that operation, beacuse there are too much image files in the directory you have selected. We're sorry.")
        End Try


    End Sub
    Public Shared Sub GoDeeper(ListViewUsed As ListView)
        Call refresh(ListViewUsed.Items.Item(ListViewUsed.SelectedItems.Item(0).Index).ToString, ListViewUsed)
    End Sub
End Class
